﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class FunctionRolesRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<FunctionRole> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.FunctionRoles
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p=>p.FunctionRoleID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public FunctionRole GetByID(int ID)
        {
            var result = from p in data.FunctionRoles
                         where p.FunctionRoleID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public IEnumerable<FunctionRole> GetByFunctionID(int ID)
        {
            var results = from p in data.FunctionRoles
                         where p.Function.FunctionID == ID
                         select p;
            return results;
        }

        public IEnumerable<FunctionRole> GetByRoleID(int ID)
        {
            var results = from p in data.FunctionRoles
                         where p.Role.RoleID == ID
                         select p;
            return results;
        }

        public void Insert(FunctionRole fr)
        {
            data.AddToFunctionRoles(fr);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}